package net.pec.geotools_svg.svg_conversion;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;


import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.geotools.data.DataUtilities;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;

public class GenerateSVGMapdocument {

	private double viewboxX,viewboxY,viewboxWidth,viewboxHeight;
	private Element svgRoot;
	private Element gSuperElement;
	private Element basemapElement;
	private double middleY;
	private double middleX;
	private boolean includeBaseMap=false;
	private String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
	private static double Buffer = 0.8;
	private int rotation = 0;
	private List layerList = new ArrayList();
	private FeatureCollection<SimpleFeatureType, SimpleFeature>  poiFC;
	
	
	
	public GenerateSVGMapdocument() {
	}
	
	
	public Document initialiseDocument(String width, String height, Document svgDoc){
		DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
				
		svgDoc = impl.createDocument(svgNS, "svg", null);		
		svgRoot = svgDoc.getDocumentElement();
		svgRoot.setAttributeNS(null, "width", width);
		svgRoot.setAttributeNS(null, "height", height);
		svgRoot.setAttributeNS(null, "id", "mapView");
		svgRoot.setAttributeNS(null, "preserveAspectRatio", "xMidYMid slice");
		svgRoot.setAttributeNS(null, "viewBox", viewboxX+ ","+viewboxY+", " + viewboxWidth + ", "+viewboxHeight );
		
		Element defs = svgDoc.createElementNS(svgNS, "defs");
		
		Element marker = svgDoc.createElementNS(svgNS, "marker");
		marker.setAttribute("id", "MidMarker");
		marker.setAttribute("viewBox", "0 0 10 10");
		marker.setAttribute("refX", "0");
		marker.setAttribute("refY", "5");
		marker.setAttribute("markerUnits", "strokeWidth");
		marker.setAttribute("markerWidth", "2");
		marker.setAttribute("markerHeight", "2");
		marker.setAttribute("stroke", "rgb(100,100,100)");
		marker.setAttribute("stroke-width", "1");
		marker.setAttribute("fill", "rgb(255,255,0)");
		marker.setAttribute("opacity", "0.8");
		marker.setAttribute("orient", "auto");
		
		Element symbolElement = svgDoc.createElementNS(svgNS, "path");
		symbolElement.setAttributeNS(null, "d", "M 0 0 L 10 5 L 0 10 Z");
		
		marker.appendChild(symbolElement);
		defs.appendChild(marker);
     
		svgRoot.appendChild(defs);
		
		gSuperElement = svgDoc.createElementNS("http://www.w3.org/2000/svg", "g");
		gSuperElement.setAttributeNS(null, "id", "allLayers");
	    
		
		
		return svgDoc;
		
	}
	
	public Document getDocument(Document svgDoc) throws Exception{
			
		
		gSuperElement.setAttributeNS(null, "transform", "rotate("+rotation+","+middleX+","+middleY+")");
		svgRoot.setAttributeNS(null, "viewBox", viewboxX+ ","+viewboxY+", " + viewboxWidth + ", "+viewboxHeight );
		
		if(basemapElement!=null){
			Element basemapGElement =  svgDoc.createElementNS("http://www.w3.org/2000/svg", "g");
			basemapGElement.setAttributeNS(null, "id", "basemapLayer");
			basemapGElement.appendChild(basemapElement);
			svgRoot.appendChild(basemapGElement);
		}
		
		svgRoot.appendChild(gSuperElement);
		
		
		
		Element gElement = svgDoc.createElementNS("http://www.w3.org/2000/svg", "g");
		gElement.setAttributeNS(null, "id", "vectorLayer");
		
		Iterator layerIt = layerList.iterator();
				
				while(layerIt.hasNext()){
					Element tempGElement = (Element) layerIt.next();								
					gElement.appendChild(tempGElement);					
				}
				gSuperElement.appendChild(gElement);
		
				
		return svgDoc;
	}
	
	public void setViewBoxAttribute(Double viewboxX, Double viewboxY, Double viewboxWidth, Double viewboxHeight){
		
		
		
		this.viewboxWidth=viewboxWidth;
		this.viewboxHeight=viewboxHeight;
		 		
		double dHW;
		System.out.println("ratio = "+viewboxWidth/viewboxHeight);
		if(this.viewboxHeight<this.viewboxWidth){
			this.viewboxHeight=this.viewboxWidth;
			System.out.println("width greater "+this.viewboxHeight+" "+this.viewboxWidth);
			dHW = this.viewboxWidth*2-this.viewboxWidth;
			
			this.viewboxWidth=this.viewboxWidth+dHW;
			this.viewboxHeight=this.viewboxHeight+dHW;
			
			this.viewboxX=viewboxX-dHW*(viewboxHeight/viewboxWidth);
			this.viewboxY=viewboxY-dHW;
			
   	   	}
		
   	   	else{
	   	   	this.viewboxWidth=this.viewboxHeight; 
	   	   	System.out.println("height greater "+this.viewboxHeight+" "+this.viewboxWidth);
	   	 
	   	   	dHW = this.viewboxWidth*2-this.viewboxWidth;
		
	   	   	this.viewboxWidth=this.viewboxWidth+dHW;
	   	   	this.viewboxHeight=this.viewboxHeight+dHW;
		
	   	   	this.viewboxX=viewboxX-dHW;
	   	   	this.viewboxY=viewboxY-dHW*(viewboxWidth/viewboxHeight);   	
   	   	}
				
	
		this.middleY=this.viewboxY+this.viewboxHeight/2;
		
		
		System.out.println(dHW);
		this.middleX=this.viewboxX+this.viewboxWidth/2;		
		
	}
	
	
	
	public double getMiddleY(){
	return middleY;
	}


	public void addGToLayerList(Element layer) {
		layerList.add(layer);
	}
	
	public void addBaseMapLayer(Element layer) {
		basemapElement=layer;
	}


	public List getLayerList() {
		return layerList;
	}
	
	public double getViewBoxY(){
		return this.viewboxY;
		}
	
	public double getViewBoxX(){
		return this.viewboxX;
		}
	
	public double getWidth(){
		return this.viewboxWidth;
		}
	
	public double getHeight(){
		return this.viewboxHeight;
		}
	
	public void setRotation(int rotation){
		this.rotation=rotation;
		}
	
	
	
}
